/*
 * app.lockenabler2.Widget.java
 * 
 * Copyright (C) 2010-11 kfv 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 */

package app.lockenabler2;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.view.View;
import android.widget.RemoteViews;

public class Widget extends AppWidgetProvider {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: Обновляем виджет
        RemoteViews updateViews = UpdateService.buildUpdate(context);

        // TODO: Помещаем виджет на домашний экран 
        ComponentName thisWidget = new ComponentName(context, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, updateViews);
    }

    public static class UpdateService extends Service {
        public static RemoteViews buildUpdate(Context context) {
            RemoteViews mRemoteViews = new RemoteViews(
                    context.getPackageName(), R.layout.widget);

            // TODO: Устанавливаем иконку
            Preferences mWidgetIcon = new Preferences("Widget_Icon", context);
            int mIcon = mWidgetIcon.getValue(0);

            if (mIcon == 0) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_red);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 1) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_orange);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 2) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_yellow);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 3) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_green);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 4) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_lightblue);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 5) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_blue);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 6) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_purple);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 7) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_white);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 8) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_black);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 9) {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon_transparent);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            } else if (mIcon == 10) {
                Preferences Custom_Icon = new Preferences("Custom_Icon", context);
                Uri mCustomIcon = Uri.parse(Custom_Icon.getValue(
                        "android.resource://app.lockenabler2/"+R.drawable.icon));
                mRemoteViews.setImageViewUri(R.id.icon, mCustomIcon);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", 0);

            } else {
                mRemoteViews.setImageViewResource(R.id.icon, R.drawable.icon);
                mRemoteViews.setInt(R.id.icon, "setBackgroundResource", R.drawable.icon_background);
            }

            // TODO: Выставляем параметры
            Preferences mShowLabel = new Preferences("Show_Label", context);
            boolean mLabel = mShowLabel.getValue(true);
            if (mLabel) {
                mRemoteViews.setViewVisibility(R.id.label, View.VISIBLE);
            } else {
                mRemoteViews.setViewVisibility(R.id.label, View.INVISIBLE);
            }

            // TODO: Задаем действия для нажатий
            Intent mGoSleep = new Intent(context, Lock.LockActivity.class);
            PendingIntent mSleep = PendingIntent.getActivity(context, 0, mGoSleep, 0);
            mRemoteViews.setOnClickPendingIntent(R.id.icon, mSleep);

            Intent mGoSettings = new Intent(context, Settings.class);
            PendingIntent mSettings = PendingIntent.getActivity(context, 0, mGoSettings, 0);
            mRemoteViews.setOnClickPendingIntent(R.id.label, mSettings);

            return mRemoteViews;
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
}
